/**
 * 
 */
package display;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import main.Agent.AgentName;

/**
 * Listens for user input on the grid, mostly for human input
 * @author ES
 */
class ButtonListener implements ActionListener {

	private JButton jButton;
	private int row = -1;
	private int col = -1;
	
	/**
	 * Listens for user input on buttons
	 * @param jButton : The button to listen to
	 */
	public ButtonListener(JButton jButton)
	{
		this.jButton = jButton;
	}
	
	/**\
	 * Listens for input on grid buttons
	 * @param jButton : The button to listen to 
	 * @param row : Row value of the button
	 * @param col : Column value of the button
	 */
	public ButtonListener(JButton jButton, int row, int col) {
		this.jButton = jButton;
		setRow(row);
		setCol(col);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(jButton.getText().equals("Start Game")){
			MainGamePanel.GetInstance().reset();
		}
		else if(arg0.getSource() == MainGamePanel.GetInstance().pauseButton){
			if(jButton.getText().equals("Pause Game")){
				jButton.setText("Unpause Game");
				MainGamePanel.GetInstance().pauseTimers(true);
			}else{
				jButton.setText("Pause Game");
				MainGamePanel.GetInstance().pauseTimers(false);
			}
		}
		else if(jButton.getIcon() == MainGamePanel.GetInstance().getEmpty() && row >= 0 && col >= 0)
		{
			//-- Allowed only in the case of Human vs Human
			if(MainGamePanel.GetInstance().isBlackPlayer() && MainGamePanel.GetInstance().getBlackAgent().getName().equals(AgentName.HUMAN))
			{
				if(MainGamePanel.GetInstance().getCurrentGrid().setElementAt(row, col, 'B') == true)//At least one element is flipped
				{
					jButton.setIcon(MainGamePanel.GetInstance().getBlack());
					MainGamePanel.GetInstance().setTurn();
					MainGamePanel.GetInstance().refreshButtons();
					System.out.println(MainGamePanel.GetInstance().getCurrentGrid().toString());
				}
			}
			else if(MainGamePanel.GetInstance().isWhitePlayer() && MainGamePanel.GetInstance().getWhiteAgent().getName().equals(AgentName.HUMAN))
			{
				if(MainGamePanel.GetInstance().getCurrentGrid().setElementAt(row, col, 'W') == true)//At least one element is flipped
				{
					jButton.setIcon(MainGamePanel.GetInstance().getWhite());
					MainGamePanel.GetInstance().setTurn();
					MainGamePanel.GetInstance().refreshButtons();
					System.out.println(MainGamePanel.GetInstance().getCurrentGrid().toString());
				}
			}
			else
			{
				//Do nothing!
			}
		}
		
		MainGamePanel.GetInstance().updateScore();
	}

	// Getters & Setters
	/**
	 * @return The row specific to this grid button listener
	 */
	public int getRow() {
		return row;
	}

	/**
	 * @param row : The row to set to this button listener
	 */
	public void setRow(int row) {
		this.row = row;
	}

	/**
	 * @return The column specific to this grid button listener
	 */
	public int getCol() {
		return col;
	}

	/**
	 * @param col : The column to set to this button listener
	 */
	public void setCol(int col) {
		this.col = col;
	}

}